/*
Author: Surpass-HC
Source: https://gitee.com/hxchuan000/clpc-1
Mail: surpass-hc@qq.com
*/
#pragma once
#include <logbase.h>
// hc circular queue struct
typedef void (*cycleQueCbkElemFree)(void *);
struct cycleQueRoot
{
    char *ptr; // queue ptr
    size_t element_size;
    size_t total_nbr;    // queue total element number.
    size_t used_nbr;     // queue used element number.
    size_t rid;          // queue read id.
    size_t wid;          // queue write id.
    __compar_fn_t cmpfn; // queue cmp fn, for qsort and bsearch
    // cycleQueCbkElemFree elemFreeFun; // user data free by themself
    pthread_mutex_t mut; // queue mutex
    int withmt;          // queue option with mutex flag.0:not, 1:use
};
typedef struct cycleQueRoot cycleQueRoot;
#define CYCLE_QUE_INIT_SIZE (8)
#define CYCLE_QUE_WARN_SIZE (1L * 1024 * 1024)
#define CYCLE_QUE_ERROR_SIZE (-1UL)
int cycleQueInit(cycleQueRoot *cqptr, __compar_fn_t cfgcmp, size_t cfgElemsize, int useMutex);
int cycleQueFree(cycleQueRoot *cqptr);
size_t cycleQueSize(cycleQueRoot *cqptr);
int cycleQuePushBack(cycleQueRoot *cqptr, void *ptr);
int cycleQuePushFront(cycleQueRoot *cqptr, void *ptr);
int cycleQuePullFront(cycleQueRoot *cqptr, void *optr);
int cycleQuePullBack(cycleQueRoot *cqptr, void *optr);
int cycleQueSort(cycleQueRoot *cqptr);
size_t cycleQueSearch(cycleQueRoot *cqptr, void *findKey);